package  
{
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author UWGameDevClub
	 */
	public class Dialog extends Sprite 
	{
		//The background for the text
		private var _backdrop:Sprite = new Sprite();
		
		//Text box
		private var _text:TextField = new TextField();
		private var _speaker:TextField = new TextField();
		private var _textFormat:TextFormat;
		private var _speakerFormat:TextFormat;
		
		//The text we want to show
		private var _finaltext:String = "";
		private var _textIndex:Number = 0;
		private static const NORMAL_TEXT_SPEED:Number = 30;
		private var _textSpeed:Number = NORMAL_TEXT_SPEED;
		private var _frameCounter:Number = 0;
		private var _pauseCounter:Number = 0;
		
		//End arrow
		private var _arrow:Bitmap = new Art.ArrowRight();
		
		public function Dialog( w:Number, h:Number ) 
		{
			redraw(w, h);
			addChild(_backdrop);
			
			//Create the dialog text field
			_text.x = 5;
			_text.y = 30;
			_text.selectable = false;
			_text.width = w - 10; _text.height = h - 10;
			_text.wordWrap = true;
			_text.text = "HELLO WORLD!";
			addChild( _text );
			
			_textFormat = new TextFormat();
			_textFormat.color = 0xFFFFFF;
			_textFormat.size = 16;
			_textFormat.font = "Calibri";
			_text.setTextFormat(_textFormat);
			
			//Create the speaker text field
			_speaker.x = _speaker.y = 0;
			_speaker.selectable = false;
			_speaker.width = Math.min( 75, w );
			_speaker.height = Math.min( 50, h );
			addChild(_speaker);
			
			_speakerFormat = new TextFormat();
			_speakerFormat.color = 0xFFFFFF;
			_speakerFormat.size = 16;
			_speakerFormat.font = "Calibri";
			_speaker.setTextFormat(_speakerFormat);
			
			addEventListener( Event.ENTER_FRAME, update );
			
			//Add arrow finish notice
			addChild( _arrow );
			_arrow.visible = false;
			_arrow.x = w - _arrow.width - 10;
			_arrow.y = h - _arrow.height - 10;
		}
		
		private function update( e:Event ):void
		{
			if ( _pauseCounter > 0 ) {
				_pauseCounter--;
				return;
			}
			//Calculate frames per letter
			var framesPerLetter:Number = Math.round(Main.FRAME_RATE / _textSpeed);
			
			_frameCounter++;
			if ( _frameCounter >= framesPerLetter ) {
				//Increment counters
				_frameCounter = 0;
				_textIndex++;
				
				//Check for special characters
				if ( _finaltext.charAt(_textIndex) == "&" ) {
					_textIndex++;
					_pauseCounter = 20;
				}
				if ( _finaltext.charAt(_textIndex) == "%" ) {
					_textSpeed = Number(_finaltext.substr( _textIndex + 1, 3 ));
					if ( _textSpeed == 0 ) {
						_textSpeed = NORMAL_TEXT_SPEED;
						_textIndex = _finaltext.length;
					}
					else _textIndex += 4;
				}
				
				//Set the string
				if ( _textIndex >= _finaltext.length ) {
					_textIndex = _finaltext.length;
					_arrow.visible = true;
				}
				_text.text = _finaltext.substr( 0, Math.floor( _textIndex ) ).replace( /(&|%\d?\d?\d?)/g, "" );
			}
			
			
			//make sure the text stays in the format we want
			_speaker.setTextFormat(_speakerFormat);
			_text.setTextFormat(_textFormat);
		}
		
		//Tells the dialog to finish the current text
		public function finish( ):void
		{
			_textIndex = _finaltext.length;
		}
		public function finished( ):Boolean
		{
			if ( _textIndex < _finaltext.length ) return false;
			else return true;
		}
		
		//Posts some text
		public function post( speaker:String, dialog:String ):void
		{
			_textSpeed = NORMAL_TEXT_SPEED;
			_arrow.visible = false;
			_speaker.text = speaker;
			_text.text = "";
			_finaltext = dialog;
			_textIndex = -1;
			
			//make sure the text stays in the format we want
			_speaker.setTextFormat(_speakerFormat);
			_text.setTextFormat(_textFormat);
		}
		
		//Redraws the backdrop
		private function redraw( w:Number, h:Number ):void
		{
			var g:Graphics = _backdrop.graphics;
			g.beginFill( 0x454555, 0.75 );
			g.drawRect(0, 0, 75, 25 );
			g.endFill();
			g.beginFill( 0x252535, 0.75 );
			g.drawRect(0, 25, w, h);
			g.endFill();
		}
	}
}